const app = getApp()

Page({
  data: {
    settings: {
      notifications: true,
      autoPlay: false,
      darkMode: false,
      dailyGoal: 10,
      reminderTime: '20:00',
      studyReminder: true,
      showInLeaderboard: true,
      saveHistory: true
    },
    showGoalDialog: false,
    showTimeDialog: false,
    tempReminderTime: '20:00',
    goalOptions: [
      { value: 5, desc: '轻松学习' },
      { value: 10, desc: '适中目标' },
      { value: 20, desc: '挑战自我' },
      { value: 30, desc: '学霸模式' }
    ]
  },

  onLoad() {
    this.loadSettings()
  },

  // 加载设置
  loadSettings() {
    const settings = wx.getStorageSync('app_settings')
    if (settings) {
      this.setData({ settings: { ...this.data.settings, ...settings } })
    }
  },

  // 保存设置
  saveSettings() {
    wx.setStorageSync('app_settings', this.data.settings)
  },

  // 切换通知设置
  toggleNotifications(e) {
    const settings = { ...this.data.settings, notifications: e.detail.value }
    this.setData({ settings })
    this.saveSettings()
    
    if (e.detail.value) {
      wx.requestSubscribeMessage({
        tmplIds: ['your_template_id'],
        success: (res) => {
          console.log('订阅消息成功:', res)
        }
      })
    }
  },

  // 切换自动播放
  toggleAutoPlay(e) {
    const settings = { ...this.data.settings, autoPlay: e.detail.value }
    this.setData({ settings })
    this.saveSettings()
  },

  // 切换夜间模式
  toggleDarkMode(e) {
    const settings = { ...this.data.settings, darkMode: e.detail.value }
    this.setData({ settings })
    this.saveSettings()
    
    // 应用主题
    if (e.detail.value) {
      wx.setNavigationBarColor({
        frontColor: '#ffffff',
        backgroundColor: '#000000'
      })
    } else {
      wx.setNavigationBarColor({
        frontColor: '#000000',
        backgroundColor: '#ffffff'
      })
    }
  },

  // 设置学习目标
  setStudyGoal() {
    this.setData({ showGoalDialog: true })
  },

  // 选择学习目标
  selectGoal(e) {
    const value = e.currentTarget.dataset.value
    const settings = { ...this.data.settings, dailyGoal: value }
    this.setData({ settings })
  },

  // 确认学习目标
  confirmGoal() {
    this.saveSettings()
    this.setData({ showGoalDialog: false })
    wx.showToast({
      title: '设置成功',
      icon: 'success'
    })
  },

  // 关闭学习目标弹窗
  closeGoalDialog() {
    this.setData({ showGoalDialog: false })
  },

  // 设置提醒时间
  setReminderTime() {
    this.setData({ 
      showTimeDialog: true,
      tempReminderTime: this.data.settings.reminderTime
    })
  },

  // 时间选择器变化
  onTimeChange(e) {
    this.setData({ tempReminderTime: e.detail.value })
  },

  // 确认提醒时间
  confirmTime() {
    const settings = { ...this.data.settings, reminderTime: this.data.tempReminderTime }
    this.setData({ settings })
    this.saveSettings()
    this.setData({ showTimeDialog: false })
    
    wx.showToast({
      title: '设置成功',
      icon: 'success'
    })
  },

  // 关闭时间弹窗
  closeTimeDialog() {
    this.setData({ showTimeDialog: false })
  },

  // 切换学习提醒
  toggleStudyReminder(e) {
    const settings = { ...this.data.settings, studyReminder: e.detail.value }
    this.setData({ settings })
    this.saveSettings()
    
    if (e.detail.value) {
      wx.requestSubscribeMessage({
        tmplIds: ['your_template_id'],
        success: (res) => {
          console.log('订阅消息成功:', res)
        }
      })
    }
  },

  // 切换排行榜显示
  toggleLeaderboard(e) {
    const settings = { ...this.data.settings, showInLeaderboard: e.detail.value }
    this.setData({ settings })
    this.saveSettings()
  },

  // 切换历史记录
  toggleHistory(e) {
    const settings = { ...this.data.settings, saveHistory: e.detail.value }
    this.setData({ settings })
    this.saveSettings()
  },

  // 清除缓存
  clearCache() {
    wx.showModal({
      title: '确认清除',
      content: '确定要清除缓存吗？这将释放存储空间。',
      success: (res) => {
        if (res.confirm) {
          wx.clearStorage({
            success: () => {
              wx.showToast({
                title: '清除成功',
                icon: 'success'
              })
            }
          })
        }
      }
    })
  },

  // 检查更新
  checkUpdate() {
    const updateManager = wx.getUpdateManager()
    
    updateManager.onCheckForUpdate((res) => {
      if (res.hasUpdate) {
        wx.showModal({
          title: '发现新版本',
          content: '是否立即更新？',
          success: (res) => {
            if (res.confirm) {
              updateManager.applyUpdate()
            }
          }
        })
      } else {
        wx.showToast({
          title: '已是最新版本',
          icon: 'success'
        })
      }
    })
  },

  // 跳转到关于页面
  goToAbout() {
    wx.navigateTo({
      url: '/pages/about/about'
    })
  }
}) 